%purpose: add instruments & indices for estimating correlations

% all possible firm pairs (n1, n2)
dev2_ind = nchoosek(1:N, 2);
N_dev2 = size(dev2_ind, 1);

%% get the actual outcome of the pairs
data_outcome2 = data_outcome(:, dev2_ind(:, 1)) ...
    .* data_outcome(:, dev2_ind(:, 2));
data_outcome2 = data_outcome2'; % N_dev2 x N_mkt
data_outcome_vec2 = data_outcome2(:);  

%% market-level fc shock draws
corr_sim_mkt = repmat(randn(N_mkt, 1, N_corr_sim), [1 N 1]);
corr_sim = reshape(corr_sim_mkt, [N*N_mkt, N_corr_sim]);
corr_sim_at = corr_sim_mkt;
corr_sim_ct = repmat(randn(1, N_mkt, 1, N_sim), [1 1 N 1]);

%% instrument
% profit lower bounds corresponding with each pair, n1
profit_lb_dev2_f1 = reshape(profit_lb(:, dev2_ind(:, 1)), [N_mkt*N_dev2 1]);

% profit lower bounds corresponding with each pair, n2
profit_lb_dev2_f2 = reshape(profit_lb(:, dev2_ind(:, 2)), [N_mkt*N_dev2 1]);

% profit upper bounds corresponding with each pair, n1
profit_ub_dev2_f1 = reshape(profit_ub(:, dev2_ind(:, 1)), [N_mkt*N_dev2 1]);

% profit upper bounds corresponding with each pair, n2
profit_ub_dev2_f2 = reshape(profit_ub(:, dev2_ind(:, 2)), [N_mkt*N_dev2 1]);

% instruments corrsponding with each product's lower and upper profit bds
corr_g_ar = [gen_iv_monte_carlo(profit_lb_dev2_f1, profit_ub_dev2_f1, ...
    profit_cutoff_corr, profit_cutoff_corr), ...
    gen_iv_monte_carlo(profit_lb_dev2_f2, profit_ub_dev2_f2, ...
    profit_cutoff_corr, profit_cutoff_corr)];

%% get indices for the pairs
all_ind = reshape(1:N_mkt*N, [N_mkt N]);
dev2_ind_f1_tr = all_ind(:, dev2_ind(:, 1)); dev2_ind_f1_tr = dev2_ind_f1_tr(:);
dev2_ind_f2_tr = all_ind(:, dev2_ind(:, 2)); dev2_ind_f2_tr = dev2_ind_f2_tr(:);

clear data_outcome2 all_ind corr_sim_mkt
